﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using PWS.Entities;


namespace PWS.DAL
{
    public class CustomerAccess
    {
        private static Customer CustomerFields(DataRow Row)
        {
            Customer c = new Customer();
            c.CustId= (int)Row["fldCustId"];
            c.CustLastName = Row["fldCustLastName"] as string;
            c.CustFirstName = Row["fldCustFirstName"] as string;
            c.CustAddress = Row["fldCustAddress"] as string;
            c.CustState = Row["fldCustState"] as string;
            c.CustCity = Row["fldCustCity"] as string;
            c.ZipCode = Row["fldCustZip"] as string;
            c.CustPhone = Row["fldCustPhone"] as string;
            return c;

        }
        private static Customer[] DataSetToCustomerArray(DataSet ds)
       {
            Customer[] customers = new Customer[ds.Tables[0].Rows.Count];
           for (int i = 0; i < ds.Tables[0].Rows.Count; ++i)
           {
                customers[i] = CustomerFields(ds.Tables[0].Rows[i]);
            }
         return customers;

        }
        public static Customer[] GetCustomers()
        {
            DataSet ds = SqlHelper.ExecuteDataset(
            ConfigurationManager.ConnectionStrings["PineDb"].
            ConnectionString, "Customers_Get_Customers");
            return DataSetToCustomerArray(ds);
        }
        public static bool InsertCustomer(Customer newCustomer)
        {
            int temp = SqlHelper.ExecuteNonQuery(
            ConfigurationManager.ConnectionStrings["PineDb"].
            ConnectionString, "Customers_Insert_Customer",
            newCustomer.CustFirstName, newCustomer.CustLastName, newCustomer.CustAddress, newCustomer.CustState, newCustomer.CustCity, newCustomer.ZipCode, newCustomer.CustPhone);
            if (temp > 0)
                return true;
            return false;
        }		


    }
}